package adfc2.tasks;

import adfc2.actions.ConstructAction;
import adfc2.core.Action;
import adfc2.core.Creature;
import adfc2.core.Item;
import adfc2.core.Task;

import static adfc2.core.World.*;

/**
 * Построить конструкцию (стену/лестницу/рампу)
 */
public class ConstructTask extends Task {

    public final short construction;
    public final int tgtxy;
    public final Item material;
    private MoveTask moving;

    public ConstructTask(Creature actor, short construction, int tgtxy, Item material) {
        super(actor);
        this.construction = construction;
        this.tgtxy = tgtxy;
        this.material = material;
    }

    @Override
    public Action nextAction() {
        if ((world().blocks[tgtxy] & MASK_CONSTRUCTION_WALL) != 0) {
            completed();
            return null;
        } else if (near(actor.getXY(), tgtxy)) {
            return new ConstructAction(actor, construction, tgtxy, material);
        } else if (actor.hasItem(material) && (world().blocks[tgtxy] & MASK_IMPASSABLE) == 0) {
            if (moving == null) {
                moving = new MoveTask(actor, tgtxy, true);
            }
            return moving.nextAction();
        } else {
            failed();
            return null;
        }
    }

    @Override
    public String shortName() {
        return "construct " + material.material().shortName() + " " + constructionName(construction);
    }

}
